<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="测评订单状态" prop="status">
        <el-select v-model="queryParams.status">
          <el-option v-for="item in dict.type.evaluation_status" :key="item.value" :label="item.label"
                     :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="orderId">
        <el-input v-model="queryParams.orderId" placeholder="请输入订单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="运单号" prop="intlTrackingNo">
        <el-input v-model="queryParams.intlTrackingNo" placeholder="请输入运单号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="卖家名称" prop="sellerSignerFullname">
        <el-select v-model="queryParams.sellerSignerFullname" placeholder="请选择卖家名称" clearable filterable>
          <el-option v-for="(dict,index) in storeList" :key="index" :label="dict" :value="dict"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-hasPermi="['evaluation:internal:add']">
        <el-button type="primary" plain size="mini" @click="openAddOrderModal">
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5" v-hasPermi="['evaluation:internal:cancel']">
        <el-button type="danger" :disabled="multiple" plain size="mini" @click="cancelOrder">
          作废测评单
        </el-button>
      </el-col>
      <el-col :span="1.5" v-hasPermi="['evaluation:internal:export']">
        <el-button type="warning" :disabled="multiple" plain icon="el-icon-download" size="mini" @click="handleExport">
          导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sellerStoreProductEvaluationList" @selection-change="handleSelectionChange"
              stripe border>
      <el-table-column type="selection" width="40" align="center"/>
      <el-table-column label="店铺名称" align="center" prop="storeName" width="180"/>
      <el-table-column label="卖家全称" align="center" prop="sellerSignerFullname" width="180"/>
      <el-table-column label="国家" align="center" prop="country" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_evaluation_country" :value="scope.row.country"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="产品主图" align="center" prop="productImg">
        <template slot-scope="scope">
          <imagePreview :src="scope.row.productImg" width="50px" height="50px"/>
        </template>
      </el-table-column>
      <el-table-column label="SKU" align="center" prop="skuCode"/>
      <el-table-column show-overflow-tooltip label="购买链接" align="center" prop="link"/>
      <el-table-column label="测评数量" align="center" prop="number" width="75"/>
      <el-table-column show-overflow-tooltip label="关键词" align="center" prop="keyWord"/>
      <el-table-column label="测评时间" align="center" prop="evaluationTime" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.evaluationTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="采购总费用" align="center" prop="totalAmount" width="180">
        <template slot-scope="scope">
          <el-input placeholder="请输入金额" size="small" v-model="scope.row.totalAmount">
            <el-button :disabled="!scope.row.totalAmount" slot="append"
                       v-if="scope.row.isEditTotalAmount"
                       @click="submitNum(scope.row, '2')" type="text" size="mini">
              确定
            </el-button>
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center" prop="orderId" width="200">
        <template slot-scope="scope">
          <el-input placeholder="请输入订单号" size="small" v-model="scope.row.orderId">
            <el-button :disabled="!scope.row.orderId" slot="append"
                       v-if="scope.row.status != 2 && scope.row.status != 3 && scope.row.orderStatus == '0'"
                       @click="submitNum(scope.row, '0')" type="text" size="mini">
              确定
            </el-button>
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="运单号" align="left" prop="intlTrackingNo" width="200">
        <template slot-scope="scope">
          <div style="margin-top: 28px;">
            <el-input placeholder="请输入运单号" size="small" v-model="scope.row.intlTrackingNo">
              <!--              <el-button :disabled="!scope.row.intlTrackingNo" slot="append"-->
              <!--                         v-if="scope.row.status == 2 || scope.row.status == 4 && scope.row.intlTrackingNoStatus == '0'"-->
              <!--                         @click="submitNum(scope.row, '1')" type="text" size="mini">确定-->
              <!--              </el-button>-->
              <el-button slot="append" @click="submitNum(scope.row, '1')"
                         v-if="scope.row.status == 2 || scope.row.status == 4 || scope.row.status == 5"
                         type="text" size="mini">确定
              </el-button>
            </el-input>
            <el-button @click="sellerShipmentForEvaluationBtn(scope.row)" type="text" size="mini">声明发货</el-button>
            <el-button @click="markShipment(scope.row)" type="text" size="mini">标记已线下发货</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.evaluation_status" :value="scope.row.status"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    <!--新增测评订单-->
    <el-dialog title="新增测评订单" :visible.sync="isOpenOrderModal" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="卖家账号" prop="storeId">
          <el-select size="small" v-model="form.storeId" clearable filterable placeholder="请选择卖家账号">
            <el-option
              v-for="(item,index1) in sellerList"
              :key="index1"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单号" prop="orderId">
          <el-input size="small" v-model="form.orderId" placeholder="请输入订单号"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {
  listSellerStoreProductEvaluation,
  getSellerStoreProductEvaluation,
  delSellerStoreProductEvaluation,
  submitOrderNumber,
  submitIntlTrackingNo,
  submitTotalAmount,
  selectStoreList,
  InvalidEvaluationOrder,
  evaluationOrderShipped,
  getSellerSignerFullname,
  addEvaluationOrder
} from "@/api/evaluation/evaluation";
import {sellerShipmentForEvaluation} from "@/api/warehouse/warehouse";

export default {
  name: "SellerStoreProductEvaluation",
  dicts: ['evaluation_status', 'sys_evaluation_country'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 在线商品测评表格数据
      sellerStoreProductEvaluationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        skuCode: undefined,
        orderId: undefined,
        intlTrackingNo: undefined,
        status: undefined,
        isCompany: 1
      },
      // 表单参数
      form: {},
      isDisAmount: false,//是否禁用采购费用
      storeList: [],//店铺列表
      sellerList: [],//卖家列表
      isOpenOrderModal: false,//是否打开新增测评订单模态框
      rules: {
        orderId: [
          {required: true, message: "订单号不能为空", trigger: "blur"}
        ],
        storeId: [
          {required: true, message: "卖家账号不能为空", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getEmpowerList()
    this.getSellerSignerFullname()
  },
  methods: {

    // 获取卖家列表
    getSellerSignerFullname() {
      getSellerSignerFullname().then(res => {
        this.sellerList = res.data;
        this.loading = false;
      });
    },
    // 获取店铺账号选择列表
    getEmpowerList() {
      this.loading = true;
      selectStoreList().then(res => {
        this.storeList = res.data;
        this.loading = false;
      });
    },
    /** 查询在线商品测评列表 */
    getList() {
      this.loading = true;
      listSellerStoreProductEvaluation(this.queryParams).then(response => {
        this.sellerStoreProductEvaluationList = response.rows;
        let list = response.rows;
        for (let i = 0; i < list.length; i++) {
          if (list[i].totalAmount) {
            list[i]['isEditTotalAmount'] = false
          } else {
            list[i]['isEditTotalAmount'] = true
          }
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.isOpenOrderModal = false;
      this.reset();
    },
    // 提交填写的单号
    submitNum(row, type) {
      this.$confirm('提交后将无法修改，是否确定提交？', '提示').then(() => {
        if (type == 0) { //订单号
          let data = {
            orderNumber: row.orderId,
            evaluationId: row.id
          }
          submitOrderNumber(data).then(res => {
            this.$message.success(res.msg)
            this.getList()
          })
        } else if (type == 2) { //总费用
          let data = {
            totalAmount: row.totalAmount,
            evaluationId: row.id
          }
          submitTotalAmount(data).then(res => {
            this.$message.success(res.msg)
            // this.isDisable
            this.isDisAmount = true
            this.getList()
          })
        } else {//运单号
          let data = {
            intlTrackingNo: row.intlTrackingNo,
            evaluationId: row.id
          }
          submitIntlTrackingNo(data).then(res => {
            this.$message.success(res.msg)
            this.getList()
          })
        }
      })
    },
    // 声明发货
    sellerShipmentForEvaluationBtn(row) {
      this.$confirm('确定要声明发货吗', '提示').then(() => {
        sellerShipmentForEvaluation({evaluationId: row.id}).then(res => {
          this.$message.success(res.msg)
          this.getList()
        })
      })
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: undefined,
        storeId: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加在线商品测评";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getSellerStoreProductEvaluation(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改在线商品测评";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          addEvaluationOrder(this.form).then(res => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除在线商品测评编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delSellerStoreProductEvaluation(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    // 打开新增测评订单弹窗
    openAddOrderModal() {
      this.reset();

      this.isOpenOrderModal = true
    },
    // 作废测评订单
    cancelOrder() {
      this.$confirm('是否确定执行数据回滚操作？', '提示').then(() => {
        let data = {
          evaluationIds: this.ids.toString()
        }
        InvalidEvaluationOrder(data).then(response => {
          this.$modal.msgSuccess(response.msg);
          this.getList();
        })
      })

    },
    // 标记声明发货
    markShipment(row) {
      this.$confirm('是否确定执行标记线下发货操作？', '提示').then(() => {
        let data = {
          evaluationId: row.id
        }
        evaluationOrderShipped(data).then(response => {
          this.$modal.msgSuccess(response.msg);
          this.getList();
        })
      })

    },
    /** 导出按钮操作 */
    handleExport() {
      const ids = this.ids.join(',');
      this.download('gather/sellerStoreProductEvaluation/export', {
        ids
      }, `sellerStoreProductEvaluation_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
